<?php
/*
 * @Author: 一品网络技术有限公司
 * @Date: 2022-06-30 11:46:30
 * @LastEditTime: 2022-09-25 22:36:47
 * @FilePath: \web\app\common\model\Rule.php
 * @Description:
 * 联系QQ:58055648
 * Copyright (c) 2022 by 东海县一品网络技术有限公司, All Rights Reserved.
 */

namespace app\admin\model;

use think\Model;
use think\facade\Db;

class Rule extends Model
{

    public static function onAfterDelete($res){
        $data = self::select();
        $sonids = self::getSon($data, $res->id);
        if ($sonids) {
            Db::name('rule')->delete($sonids);
        }
    }


    //创建节点菜单
    public static function createNode($data)
    {
        //内容模型不用节点
        if ($data['type'] == 'classic') {
            return;
        }
        $name = ucfirst($data['name']);
        Rule::insertGetId([
            'pid' => $data['rule_id'],
            'name' => $name,
            'path' => '/model',
            'title' => $data['alias'],
            'sort' => $data['sort'],
            'icon' => $data['icon'],
            'status' => 1,
            'mod_id' => $data['id']
        ]);
    }
    //获取所有子类
    static private function getSon($data, $id)
    {
        static $ids = [];
        foreach ($data as $k => $v) {
            if ($v->pid == $id) {
                $ids[] = $v->id;
                self::getSon($data, $v->id);
            }
        }
        return $ids;
    }
    public function mod()
    {
        return $this->belongsTo(Mod::class);
    }
    //父目录
    public function setPathAttr($value,$data)
    {
        if ($data['type'] == 1) {
            return '/dir';
        } else {
            return $value;
        }
    }

}
